package tower.ai.base.chat.service;

import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.moonshot.MoonshotChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;

@Slf4j
@Service
public class MoonshotChatService {

    @Autowired
    private MoonshotChatModel chatModel;

    @Autowired
    private ChatMessageHistoryService messageHistoryService;

    @Value("classpath:/prompt/easyAssent.st")
    private Resource easyAssentST;


    private List<String> getMessage(String uid) {
        return messageHistoryService.getMessage(uid);
    }

    public ChatResponse chat(String message, String history, String uid) {
        List<String> messages = getMessage(uid);
        if(StringUtils.isNotBlank(uid) && StringUtils.isNotBlank(history)){
            messages = messageHistoryService.addMessage(uid, MessageType.ASSISTANT, history, messages);
        }
        ChatClient chatClient = ChatClient.create(chatModel);
        ChatResponse callResponse = chatClient.prompt()
                .system(sysSpec -> sysSpec
                        .text(easyAssentST)
                        .param("language", "中文"))
                .messages(messageHistoryService.buildMessage(messages))
                .user(message)
                .call()
                .chatResponse();
        if(StringUtils.isNotBlank(uid)){
            messages = messageHistoryService.addMessage(uid, MessageType.USER, message, messages);
        }
        log.info("-返回内容->" + callResponse.getResult().getOutput().getContent());
        return callResponse;
    }

    public Flux<ChatResponse> streamChat(String message, String history, String uid) {
        List<String> messages = getMessage(uid);
        if(StringUtils.isNotBlank(uid) && StringUtils.isNotBlank(history)){
            messages = messageHistoryService.addMessage(uid, MessageType.ASSISTANT, history, messages);
        }
        ChatClient chatClient = ChatClient.create(chatModel);
        Flux<ChatResponse> chatResponse =  chatClient.prompt()
                .system(sysSpec -> sysSpec
                .text(easyAssentST)
                .param("language", "中文"))
                .messages(messageHistoryService.buildMessage(messages))
                .user(message)
                .stream()
                .chatResponse();
        /** 通过输出可以知道Flux是一种流.且一旦获取内容才会发起请求进行连接,因而下方日志打印内容和页面的不一致.
            final StringBuilder sb = new StringBuilder("");
            chatResponse.toStream().forEach(chatObj -> sb.append(chatObj.getResult().getOutput().getContent()));
            log.info("---->{}",sb.toString().replace("\n", "").replace("\r",""));
         */
        if(StringUtils.isNotBlank(uid)){
            messages = messageHistoryService.addMessage(uid, MessageType.USER, message, messages);
        }
        return chatResponse;
    }

}
